<template>
  <div class="settings-page">
    <div class="container page">
      <div class="row">
        <div class="col-md-6 offset-md-3 col-xs-12">
          <h1 class="text-xs-center">Your Settings</h1>

          <ul class="error-messages">
            <li>That name is required</li>
          </ul>

          <form>
            <fieldset>
              <fieldset class="form-group">
                <input class="form-control" type="text" placeholder="URL of profile picture"
                  v-model="update.user.image" />
              </fieldset>
              <fieldset class="form-group">
                <input class="form-control form-control-lg" type="text" placeholder="Your Name" />
              </fieldset>
              <fieldset class="form-group">
                <textarea class="form-control form-control-lg" rows="8" placeholder="Short bio about you"
                  v-model="update.user.bio"></textarea>
              </fieldset>
              <fieldset class="form-group">
                <input class="form-control form-control-lg" type="text" placeholder="Email"
                  v-model="update.user.email" />
              </fieldset>
              <fieldset class="form-group">
                <input class="form-control form-control-lg" type="password" placeholder="New Password" />
              </fieldset>
              <button class="btn btn-lg btn-primary pull-xs-right" @click.prevent="updateInfo()">Update
                Settings</button>
            </fieldset>
          </form>
          <hr />
          <button class="btn btn-outline-danger">Or click here to logout.</button>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import {  ref } from 'vue';
import type { LoginRsp, UserUpdateReq } from '../models';
import axios from '../utils/request';
import { useRouter } from 'vue-router';
import { useUserStore } from '../store/user';

const router = useRouter();
let store = useUserStore();
const { setUserInfo } = store;
let update = ref<UserUpdateReq>({
  user: {
    email: '',
    bio: '',
    image: '',
    token: '',
    username: ''
  }
})
let updated_user = ref<LoginRsp>({
  user: {
    email: '',
    token: '',
    username: '',
    bio: '',
    image: ''
  }
})

function updateInfo() {
  axios.put('/user', update.value).then(res => {
    // console.log(res.data);
    updated_user.value = res.data;
    setUserInfo(updated_user.value.user)
    router.push({ name: 'profile', params: { username: updated_user.value.user.username } });

  })
}


</script>
<style scoped></style>
